package demo;

import java.util.Arrays;
import java.util.Comparator;

class Student1 {
    public String name;
    public int age;
    public int score;

    public Student1(String name,int age,int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

}
class AgeComparator implements Comparator<Student1> {

    @Override
    public int compare(Student1 o1,Student1 o2) {
        return o1.age - o2.age;//升序
        //return o2.age; -o1.age;//降序
    }
}
class ScoreComparator implements Comparator<Student1> {


    @Override
    public int compare(Student1 o1, Student1 o2) {
        return o1.score - o2.score;
    }
}

class NameComparator implements Comparator<Student1> {

    @Override
    public int compare(Student1 o1, Student1 o2) {
        return o1.name.compareTo(o2.name);
    }
}


public class Test2 {
    public static void main(String[] args) {
        Student1[] students = new Student1[3];
        students[0] = new Student1("zhangsan",10,19);
        students[1] = new Student1("lisi",11,35);
        students[2] = new Student1("wangwu",15,47);

        //比较器
        AgeComparator ageComparator = new AgeComparator();

        ScoreComparator scoreComparator = new ScoreComparator();

        NameComparator nameComparator = new NameComparator();

        //Arrays.sort(students,ageComparator);
        //Arrays.sort(students,scoreComparator);
        Arrays.sort(students,nameComparator);

        System.out.println(Arrays.toString(students));
    }
}
